package Groep1;

import java.util.*;
/**
 * Opstarten van de applicatie.
 */
class Main {

    /**
     * Entry-point voor applicatie.
     * @param args command-line argumenten
     */
    public static void main(String[] args) throws Exception {
        //Open parser op standaard input stream
        Parser parser=new Parser(System.in);
        //Print het resultaat van parsen
        Map<String,NonTerminal> map= parser.parseRoot();
        Iterator<NonTerminal> i = map.values().iterator();
        boolean failed=false;
        System.out.println("Calculating sets..");
        while(i.hasNext()) {
            NonTerminal nt = i.next();
            nt.calculateSets();
        }
        System.out.println();
        i= map.values().iterator();
        while(i.hasNext()) {
            NonTerminal nt = i.next();
            if(nt.passesRule1()) {
                printGreen(nt.getName()+": passed rule 1 (startersets must be disjunct).");
                System.out.println();
                if(nt.passesRule2()) {
                    printGreen(nt.getName()+": passed rule 2 (If Starters("+nt.getName()+") contains E, Starter set and Follow set must be disjunct.)");
                }
                else {
                    printRed(nt.getName()+": *FAILED* Rule 2 check. (If Starters("+nt.getName()+") contains E, Starter set and Follow set must be disjunct.)");
                    failed=true;
                }
            }
            else {
                printRed(nt.getName()+": *FAILED* Rule 1 check. (startersets must be disjunct).");
                failed=true;
            }
            System.out.println();
        }
        if(failed) {
            printRed("BNF fails check. is not LL(1).");
            return;
        }
        else {
            printGreen("BNF is LL(1) :D");
        }
    }

    public static void printGreen(String text) {
        System.out.println("\033[1;32m"+text+"\033[0m");
    }

    public static void printRed(String text) {
        System.out.println("\033[1;31m"+text+"\033[0m");
    }
}
